﻿using ConsoleApp1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mainform
{
    public partial class OrderEditForm : Form
    {
        private readonly Order _originalOrder;
        private readonly OrderService _orderService;
        private Order _editingOrder;
        private readonly BindingSource _detailBindingSource = new BindingSource();

        public OrderEditForm(Order order, OrderService service)
        {
            InitializeComponent();
            _originalOrder = order;
            _orderService = service;
            InitializeData();
            SetupDataBinding();
        }

        private void InitializeData()
        {
            _editingOrder = _originalOrder == null
                ? new Order("", new Customer("", ""), DateTime.Now)
                : (Order)_originalOrder.Clone(); // 添加类型转换

            _detailBindingSource.DataSource = _editingOrder.Details;
        }

        private void SetupDataBinding()
        {
            // 订单绑定
            txtOrderId.DataBindings.Add("Text", _editingOrder, "OrderId");
            txtCustomerId.DataBindings.Add("Text", _editingOrder.Customer, "CustomerId");
            txtCustomerName.DataBindings.Add("Text", _editingOrder.Customer, "Name");
            dtpOrderDate.DataBindings.Add("Value", _editingOrder, "OrderDate");

            // 明细绑定
            dataGridViewDetails.DataSource = _detailBindingSource;

            // 商品下拉框数据源
            cmbProducts.DataSource = GetSampleProducts();
            cmbProducts.DisplayMember = "Name";
            cmbProducts.ValueMember = "ProductId";
        }

        private List<Product> GetSampleProducts()
        {
            return new List<Product>
        {
            new Product("P001", "笔记本电脑", 5999),
            new Product("P002", "手机", 3999),
            new Product("P003", "耳机", 299)
        };
        }

        // 添加明细按钮
        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (cmbProducts.SelectedItem is Product selectedProduct)
            {
                var quantity = (int)nudQuantity.Value;
                var detail = new OrderDetails(selectedProduct, quantity);

                try
                {
                    _editingOrder.AddDetail(detail);
                    _detailBindingSource.ResetBindings(false);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        // 删除明细按钮
        private void btnRemoveDetail_Click(object sender, EventArgs e)
        {
            if (dataGridViewDetails.CurrentRow?.DataBoundItem is OrderDetails detail)
            {
                _editingOrder.RemoveDetail(detail.Product);
                _detailBindingSource.ResetBindings(false);
            }
        }

        // 保存按钮
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (_originalOrder == null)
                {
                    _orderService.AddOrder(_editingOrder);
                }
                else
                {
                    _orderService.UpdateOrder(_editingOrder);
                }

                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
